﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Police.Svn;
using Police.Configuration;
using System.IO;

namespace Police
{
    class Program
    {
        protected static readonly ILog log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run(args);
        }

        public void Run(string[] args)
        {
            if (args.Length != 2)
            {
                CancelCommit();
            }
            else
            {
                String repositoryPath = args[0];
                String transactionId = args[1];

                IPreCommitValidator preCommitValidator = new PreCommitValidator();
                PreCommitValidationResponse response = preCommitValidator.Work(transactionId, repositoryPath);
                if (response.IsError)
                {
                    // Print errors, these informations will be visibile inside 
                    // AnkhSVN plugin (or anything else).
                    foreach (String error in response.Errors)
                    {
                        Console.Error.WriteLine(error);
                    }
                    CancelCommit();
                }
                else
                {
                    AllowCommit();
                }
            }
        }

        private void AllowCommit()
        {
            Environment.Exit(0);
        }

        private void CancelCommit()
        {
            Environment.Exit(1);
        }
    }
}
